/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.report;

import connectivity.QueryManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Product;
import model.SoldProduct;
import view.chart.Chart;
import view.chart.ChartValue;
import view.chart.PieChart;
import view.component.DateCombobox;

/**
 *
 * @author Kevin
 */
public class ProductsSoldPanel extends JPanel
{
    private DateCombobox cmbFromDate;
    private DateCombobox cmbToDate;
    
    private Chart chart;
    private DefaultTableModel productsTblModel;
    
    public ProductsSoldPanel()
    {
        super();
        
        setLayout(new BorderLayout(5, 0));
        
        initComponents();
    }

    private void initComponents()
    {
        JLabel lblFrom = new JLabel("Van");
        JLabel lblTo = new JLabel("Tot");
        
        cmbFromDate = new DateCombobox();
        cmbToDate = new DateCombobox();
        
        JButton btnSearch = new JButton("Zoeken");
        btnSearch.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e)
            {
                updateTableAndChart();
            }
        });
        
        Box hBox = Box.createHorizontalBox();
        hBox.add(lblFrom);
        hBox.add(cmbFromDate);
        
        hBox.add(lblTo);
        hBox.add(cmbToDate);
        
        hBox.add(btnSearch);
        
        add(hBox, BorderLayout.PAGE_START);
        
        productsTblModel = new DefaultTableModel();
        productsTblModel.addColumn("Product");
        productsTblModel.addColumn("Verkocht");
        
        JTable tblProducts = new JTable(productsTblModel);
        JScrollPane scrollPane = new JScrollPane(tblProducts);
        
        chart = new PieChart();
        
        JPanel chartAndTabelContainer = new JPanel();
        chartAndTabelContainer.add(scrollPane);
        chartAndTabelContainer.add(chart);
        chartAndTabelContainer.setLayout(
                new GridLayout(1, 1)
            );
        
        add(chartAndTabelContainer, BorderLayout.CENTER);
    }
    
    private void updateTableAndChart()
    {
        Calendar c = GregorianCalendar.getInstance();
        c.setTime(cmbFromDate.getDate());       
        
        long start = c.getTimeInMillis() / 1000L;
        
        c.setTime(cmbToDate.getDate());
        
        long end = c.getTimeInMillis() / 1000L;
        
        QueryManager queryManager = WinkelApplication.getQueryManager();
        
        List<SoldProduct> soldProducts = 
                queryManager.getSoldProductsBetween(start, end);        
        
        // Clear all of the rows in the table
        productsTblModel.setRowCount(0);
        
        ChartValue[] chartValues = new ChartValue[soldProducts.size()];
        int index = 0;
        
        Set<Integer> usedColors = new HashSet<Integer>();
        for (SoldProduct sp : soldProducts)
        {
            Product p = sp.getProduct();
            
            productsTblModel.addRow(new Object[] {
                p.getName(), sp.getAmount()
            });
            
            int colorValue = (int) (Math.random() * 0xFFFFFF);
            
            while (usedColors.contains(colorValue))
                colorValue = (int) (Math.random() * 0xFFFFFF);
            
            usedColors.add(colorValue);
            
            chartValues[index++] = new ChartValue(
                p.getName(), 
                new Color(colorValue),
                sp.getAmount()
            );
        }
        
        chart.setValues(chartValues);
    }
}


